package cn.edu.guet.controller;


import cn.edu.guet.bean.shopping.Shopping;
import cn.edu.guet.service.shopping.IShoppingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("shop")
public class ShoppingController {

    @Autowired
    private IShoppingService shoppingService;

    @Autowired
    private Shopping shopping;

    @GetMapping("shopping")
    public String shopping(HttpServletRequest request,Model model){
        String bname=request.getParameter("bname");
        List<Shopping> shoppings=shoppingService.shopping(bname);
        model.addAttribute("shoppings",shoppings);
        System.out.println("商品xxxxx：" + shoppings.get(0).getBid());
//        return "shopping";
        model.addAttribute("page","shopping");
        return "forward:/main";
    }

    @GetMapping("findbyuserid")
    @ResponseBody
    public List<Shopping> findbyuserid(HttpServletRequest request, Integer userid){
        Integer loginuserid=(Integer)request.getSession().getAttribute("userid");
        List<Shopping> totalgrade=shoppingService.findbyuserid(loginuserid);
        return totalgrade;
    }

    @RequestMapping("buybuy")
    @ResponseBody
    public String buybuy(HttpServletRequest request,int bprice,int bid,Model model) {
        Integer loginuserid=(Integer)request.getSession().getAttribute("userid");
        if (shoppingService.selectIsBuy(loginuserid+"",bid+"")==null){//这里等于空说明此用户没有这个徽章
            List<Shopping> totalgrade=shoppingService.findbyuserid(loginuserid);
            int total = totalgrade.get(0).getTotalgrade();
            //更新
            shopping.setBid(bid);
            shopping.setBprice(bprice);
            shopping.setTotalgrade(total);
            shopping.setUserid(loginuserid);
            shoppingService.update(shopping);
            shoppingService.insert(shopping);
            return "true";
        }else{
            return "false";
        }
    }


}
